/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
  \file gpsPage.h

  \author Cory Dixon
*/

#include "gpsPage.h"
#include "vcConfig.h"

extern GuiStruct    guiStr;

bool GPSPage::create_page(Client *client, GtkBuilder *builder)
{
	if( !client ) {
		cout << "GPSPage::create_page -- invalid client " << endl;
		return false;
	}
	if( page ) {
		cout << "GPSPage::create_page -- page has already been created" << endl;
		return true;
	}
	if( !builder ) {
		cout << "GPSPage::create_page -- invalid xml builder" << endl;
		return false;
	}

	page_client = client;
	page        = GTK_WIDGET (gtk_builder_get_object(builder, "gps_page"));
	gps_lat     = GTK_WIDGET (gtk_builder_get_object(builder, "gps_gps_lat"));
	gps_lon     = GTK_WIDGET (gtk_builder_get_object(builder, "gps_gps_lon"));
	gps_heading = GTK_WIDGET (gtk_builder_get_object(builder, "gps_gps_heading"));
	gps_height  = GTK_WIDGET (gtk_builder_get_object(builder, "gps_gps_height"));
	gps_speed   = GTK_WIDGET (gtk_builder_get_object(builder, "gps_gps_speed"));
	gps_pdop    = GTK_WIDGET (gtk_builder_get_object(builder, "gps_gps_pdop"));
	gps_sat     = GTK_WIDGET (gtk_builder_get_object(builder, "gps_gps_sat"));
	
	use_gps     = GTK_WIDGET (gtk_builder_get_object(builder, "use_gps"));

	// make sure we got all of our widgets
	if( !(page && gps_lat && gps_lon && gps_heading && gps_height && gps_speed && gps_pdop && gps_sat && use_gps) ) {
		page = NULL;
		return false;
	}

	g_signal_connect(G_OBJECT(use_gps), "clicked", G_CALLBACK(GPSPage::use_gps_clicked), this);

	return true;
}

void GPSPage::update()
{
	if( !page_client ) {
		cout << " - invalid page_client" << endl;
		return;
	}
	//-- See if the gps_page has been initialized
	if( page == NULL ) {
		cout << "GPSPage::update -- page has not been created" << endl;
		return;
	}

	// don't do work if we don't need too
	if( !GTK_WIDGET_VISIBLE( page ) ) {
		return;
	}

	gchar *text;

	// lat
	text = g_strdup_printf ("%.7f", page_client->telemetry.gps.pos.lat);
	gtk_entry_set_text (GTK_ENTRY (gps_lat), text);
	g_free (text);
		
	// lon
	text = g_strdup_printf ("%.7f", page_client->telemetry.gps.pos.lon);
	gtk_entry_set_text (GTK_ENTRY (gps_lon), text);
	g_free (text);

	// heading
	text = g_strdup_printf ("%.1f", page_client->telemetry.gps.track);
	gtk_entry_set_text (GTK_ENTRY (gps_heading), text);
	g_free (text);
	
	// height
	text = g_strdup_printf ("%.1f", page_client->telemetry.gps.pos.alt);
	gtk_entry_set_text (GTK_ENTRY (gps_height), text);
	g_free (text);
	
	// speed
	text = g_strdup_printf ("%.1f", page_client->telemetry.gps.sog);
	gtk_entry_set_text (GTK_ENTRY (gps_speed), text);
	g_free (text);
	
	// pdop
	text = g_strdup_printf ("%.1f", page_client->telemetry.gps.accuracy.pdop);
	gtk_entry_set_text (GTK_ENTRY (gps_pdop), text);
	g_free (text);
	
	// sats
	text = g_strdup_printf ("%i", page_client->telemetry.gps.accuracy.sats);
	gtk_entry_set_text (GTK_ENTRY (gps_sat), text);
	g_free (text);
	
	Client *me = guiStr.shm_ptr->clients.getClient(0);
	if(strcmp(page_client->name.c_str(),me->telemetry_client.id) == 0)
		set_good_state(use_gps);
	else
		set_rx_state(GTK_WIDGET(use_gps),1);

	// redraw page
	gtk_widget_queue_draw(page);	
}

void GPSPage::use_gps_clicked(GtkButton *button, gpointer data)
{
	GPSPage *gps_page = (GPSPage *)data;
	if( !gps_page ){ 
		cout << "!gps_page" << endl;
		return;
	}

	gps_page->use_node_gps();

	return;
}

void GPSPage::use_node_gps() {
	cout << "USING TELEM FROM CLIENT " << page_client->name << endl;
	Client *me = guiStr.shm_ptr->clients.getClient(0);
	set_sent_state(GTK_WIDGET(use_gps));
	strcpy(me->telemetry_client.id,page_client->name.c_str());
}
